package com.yuju.controller.group;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.yuju.common.PageModel;
import com.yuju.common.Result;
import com.yuju.common.fileld.validator.FieldValidator;
import com.yuju.controller.base.BaseController;
import com.yuju.enums.ReturnCodeEnum;
import com.yuju.enums.ReturnStatusEnum;
import com.yuju.group.enums.GoodTypeReturnCodeEnum;
import com.yuju.group.model.GoodTypeModel;
import com.yuju.group.service.IGoodTypeService;
import com.yuju.util.Utils;
import com.yuju.vo.group.GoodTypeVO;

/**
 * 
 * 类名：GoodTypeController  <br />
 *
 * 功能：商品分类相关接口
 *
 * @author : zhousc <br />
 * @Date : 2017年11月30日 上午9:00:34  <br />
 * @version : 1.1 <br />
 */
@Controller
@RequestMapping("goodType")
public class GoodTypeController extends BaseController<GoodTypeVO, GoodTypeModel> {

	@Autowired
	private IGoodTypeService goodTypeService;
	
	/**
	 * 
	 * 功能：商品分类列表<br/>
	 *
	 * @author zhousc
	 * @Date 2017年11月30日 上午9:03:06 <br/>
	 */
	@RequestMapping("list")
	@ResponseBody
	public Result list(@RequestBody JSONObject obj) {
		GoodTypeVO goodTypeVO = obj.toJavaObject(GoodTypeVO.class);
		// 参数校验
		Map<String, String> fail = FieldValidator.validateAll(goodTypeVO, "pager", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Result result = new Result();
		PageModel pageModel = goodTypeService.pageList(convert(goodTypeVO), goodTypeVO.getPage(), goodTypeVO.getPageSize());
		if (Utils.isNotNull(pageModel) && Utils.isNotNull(pageModel.getRows())) {
			result.setPageModel(pageModel);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(GoodTypeReturnCodeEnum.GOODTYPE_QUERY_SUCCESS.getCode());
			result.setReturnMsg(GoodTypeReturnCodeEnum.GOODTYPE_QUERY_SUCCESS.getDesc());
		} else {
			result = new Result();
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
			result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
		}
		return result;
	}
	
	/**
	 * 
	 * 功能：新增商品分类<br/>
	 *
	 * @author zhousc
	 * @Date 2017年11月30日 上午9:03:32 <br/>
	 */
	@RequestMapping("save")
	@ResponseBody
	public Result save(@RequestBody JSONObject obj) {
		GoodTypeVO goodTypeVO = obj.toJavaObject(GoodTypeVO.class);
		Map<String, String> fail = FieldValidator.validateAll(goodTypeVO, "save", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Result result = new Result();
		int rows = goodTypeService.save(convert(goodTypeVO));
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(GoodTypeReturnCodeEnum.GOODTYPE_SAVE_SUCCESS.getCode());
			result.setReturnMsg(GoodTypeReturnCodeEnum.GOODTYPE_SAVE_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(GoodTypeReturnCodeEnum.GOODTYPE_SAVE_FAILURE.getCode());
			result.setReturnMsg(GoodTypeReturnCodeEnum.GOODTYPE_SAVE_FAILURE.getDesc());
		}
		return result;
	}
	
	/**
	 * 
	 * 功能：更新商品分类<br/>
	 *
	 * @author zhousc
	 * @Date 2017年11月30日 上午9:03:48 <br/>
	 */
	@RequestMapping("update")
	@ResponseBody
	public Result update(@RequestBody JSONObject obj) {
		GoodTypeVO goodTypeVO = obj.toJavaObject(GoodTypeVO.class);
		Map<String, String> fail = FieldValidator.validateAll(goodTypeVO, "update", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Result result = new Result();
		int rows = goodTypeService.updateSelective(convert(goodTypeVO));
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(GoodTypeReturnCodeEnum.GOODTYPE_UPDATE_SUCCESS.getCode());
			result.setReturnMsg(GoodTypeReturnCodeEnum.GOODTYPE_UPDATE_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(GoodTypeReturnCodeEnum.GOODTYPE_UPDATE_FAILURE.getCode());
			result.setReturnMsg(GoodTypeReturnCodeEnum.GOODTYPE_UPDATE_FAILURE.getDesc());
		}
		return result;
	}

	/**
	 * 
	 * 功能：删除商品分类<br/>
	 *
	 * @author zhousc
	 * @Date 2017年11月30日 上午9:04:02 <br/>
	 */
	@RequestMapping("delete")
	@ResponseBody
	public Result delete(@RequestBody JSONObject obj) {
		GoodTypeVO goodTypeVO = obj.toJavaObject(GoodTypeVO.class);
		Map<String, String> fail = FieldValidator.validateAll(goodTypeVO, "del", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Result result = new Result();
		int rows = goodTypeService.del(goodTypeVO.getId());
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(GoodTypeReturnCodeEnum.GOODTYPE_DEL_SUCCESS.getCode());
			result.setReturnMsg(GoodTypeReturnCodeEnum.GOODTYPE_DEL_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(GoodTypeReturnCodeEnum.GOODTYPE_DEL_FAILURE.getCode());
			result.setReturnMsg(GoodTypeReturnCodeEnum.GOODTYPE_DEL_FAILURE.getDesc());
		}
		return result;
	}
	
	/**
	 * 
	 * 功能：根据ID获取商品分类信息<br/>
	 *
	 * @author zhousc
	 * @Date 2017年11月30日 上午9:04:15 <br/>
	 */
	@RequestMapping("get")
	@ResponseBody
	public Result get(@RequestBody JSONObject obj) {
		GoodTypeVO goodTypeVO = obj.toJavaObject(GoodTypeVO.class);
		Map<String, String> fail = FieldValidator.validateAll(goodTypeVO, "get", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Result result = new Result();
		GoodTypeModel GoodTypeModel = goodTypeService.findById(goodTypeVO.getId());
		if (Utils.isNotNull(GoodTypeModel)) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setData(GoodTypeModel);
			result.setReturnCode(GoodTypeReturnCodeEnum.GOODTYPE_QUERY_SUCCESS.getCode());
			result.setReturnMsg(GoodTypeReturnCodeEnum.GOODTYPE_QUERY_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
			result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
		}
		return result;
	}

	@RequestMapping("selectbyname")
	@ResponseBody
	public Result selectbyname(@RequestBody JSONObject obj) {
		GoodTypeVO goodTypeVO = obj.toJavaObject(GoodTypeVO.class);
		Map<String, String> fail = FieldValidator.validateAll(goodTypeVO, "selectbyname", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Result result = new Result();
		boolean selectByName = goodTypeService.selectByName(convert(goodTypeVO));
		if (selectByName) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(ReturnCodeEnum.VALID_REPEAT_SUCCESS.getCode());
			result.setReturnMsg(ReturnCodeEnum.VALID_REPEAT_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.VALID_REPEAT_FAILURE.getCode());
			result.setReturnMsg(ReturnCodeEnum.VALID_REPEAT_FAILURE.getDesc());
		}
		return result;
	}
	/**
	 * 
	 * 功能：下拉<br/>
	 *
	 * @author zhousc
	 * @Date 2017年12月27日 上午9:50:21 <br/>
	 */
	@RequestMapping("goodTypeDropdownList")
	@ResponseBody
	public Result goodTypeDropdownList(@RequestBody JSONObject obj) {
		GoodTypeVO goodTypeVO = obj.toJavaObject(GoodTypeVO.class);
		Result result = new Result();
		 List<GoodTypeModel> list = goodTypeService.list(convert(goodTypeVO));
		if (Utils.isNotNull(list)) {
			result.setData(list);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(GoodTypeReturnCodeEnum.GOODTYPE_QUERY_SUCCESS.getCode());
			result.setReturnMsg(GoodTypeReturnCodeEnum.GOODTYPE_QUERY_SUCCESS.getDesc());
		} else {
			result = new Result();
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
			result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
		}
		return result;
	}
	
}

